package com.example.myproject.suanfa.Comparable;

import java.util.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by xi on 2018/9/15.
 */
public class TestComparable {

    //底层是a2和a1比较，a1是第2个参数，
    //如果a2比a1<0的话 ，就交换代表的是降序
    //f (c.compare(a[runHi++], a[lo]) < 0) { // Descending
    private static void compare1(){
        List<Integer> list = new ArrayList<Integer>();
        list.add(5);
        list.add(8);
        list.add(3);
        list.add(10);
        //二分叉排序
        Collections.sort(list, new Comparator<Integer>() {
            @Override
            public int compare(Integer a1, Integer a2) {
                System.out.println("a1==" + a1 + ",比较a2=" + a2);
                //a1>a2的话，应该是大于0的升序的情况
                return -1;
            }
        });
        Iterator iterator_reverse=list.iterator();
        while(iterator_reverse.hasNext()){
            System.out.println(   iterator_reverse.next());
        }
    }

    public static  void Sorter(){
        List<String> strs = new ArrayList<String>();
        strs.add("2");
        strs.add("1路");
        strs.add("Y1路");
        strs.add("乙");
        strs.add("甲");
        strs.add("丙");
        System.out.println(strs);
        // 用了一个Comparetor
        Comparator<String> com = new Comparator<String>() {
            public int compare(String o1, String o2) {
                // reture 0 则不改变位置，return 1或者其他正数，升到前面， return -1 排到最前面去
                if (o1 == o2) {
                    return 0;
                }
                String num1 = "0";
                String num2 = "0";
                Pattern p = Pattern.compile("[0-9]+");
                Matcher m = p.matcher(o1);
                if(m.find()){
                    MatchResult mr = m.toMatchResult();
                    num1 = mr.group(0);
                }else {
                    System.out.println("2s1=="+o1+",s2=="+o2);
                    return -1;
                }
                Pattern p1 = Pattern.compile("[0-9]+");
                Matcher m1 = p1.matcher(o2);
                if(m1.find()){
                    MatchResult mr=m1.toMatchResult();
                    num2 = mr.group(0);
                }else {
                    return -1;
                }
                System.out.println("1s1=="+o1+",num1"+num1+",s2=="+o2+",num2=="+num2);
                return Integer.parseInt(num1) - Integer.parseInt(num2);
            }
        };
        Collections.sort(strs, com);
        System.out.println(strs);

    }

    public static void main(String[] args){
        compare1();
    }
}
